﻿using System;
using System.AddIn;
using System.Collections.Generic;
using AddInActorView;

namespace dotSimpl.wlanner.AddIns.ProxySettings
{
    /// <summary>
    /// Add-in for setting/disabling internet proxy.
    /// </summary>
    [AddIn("Internet Proxy Settings Changer", Version = "0.1")]
    public class InternetProxyChanger : ActorView
    {
        #region Constants

        //settings names.
        private const string enabled = "Enabled";
        private const string proxyAddress = "Address";
        private const string proxyExceptions = "Exceptions";

        #endregion

        #region Properties

        /// <summary>
        /// Gets this add-in name.
        /// </summary>
        public override string InternalName
        {
            get { return this.ToString(); }
        }

        /// <summary>
        /// Gets this add-in compatbility version.
        /// </summary>
        public override string CompatibilityVersion
        {
            get { return "V0.0.1"; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the initial settings to fill user interface.
        /// </summary>
        public override KeyValuePair<string, string>[] GetInitialSettins()
        {
            List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();

            result.Add(new KeyValuePair<string, string>(enabled, false.ToString()));
            result.Add(new KeyValuePair<string, string>(proxyAddress, string.Empty));
            result.Add(new KeyValuePair<string, string>(proxyExceptions, string.Empty));

            return result.ToArray();
        }

        /// <summary>
        /// Applies settigns to system.
        /// </summary>
        public override void ApplySettings(KeyValuePair<string, string>[] settings, Guid networkCard)
        {
            Dictionary<string, string> s = new Dictionary<string, string>();
            for (int i = 0; i < settings.Length; i++)
            {
                s.Add(settings[i].Key, settings[i].Value);
            }

            if (bool.Parse(s[enabled]))
            {
                if (!string.IsNullOrEmpty(s[proxyExceptions]))
                {
                    ProxySettingsHelper.SetProxy(s[proxyAddress], s[proxyExceptions]);
                }
                else
                {
                    ProxySettingsHelper.SetProxy(s[proxyAddress]);
                }
            }
            else
            {
                ProxySettingsHelper.UnsetProxy();
            }
        }

        public override string[] GetSettingNames()
        {
            return new string[] { enabled, proxyAddress, proxyExceptions };
        }

        #endregion
    }
}
